#include "stdafx.h"
#include "InputLayouts.h"
#include "HLSLEffect.h"
#include "GlobalAppData.h"

using namespace mgfx;
using namespace mcore;

ID3D11InputLayout* VertexPosition::InputLayout        = NULL;
ID3D11InputLayout* VertexPositionTexture::InputLayout = NULL;

void InputLayouts::init()
{
	ID3D11Device * device = GlobalAppData::Instance.Device;

	D3DX11_PASS_SHADER_DESC vsPassDesc;
    D3DX11_EFFECT_SHADER_DESC vsDesc;

	{
		const D3D11_INPUT_ELEMENT_DESC positionLayout[] =
		{
			{ "POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		HLSLEffect::createFromFile("LayoutPosition.fx")->getEffect()->GetTechniqueByIndex(0)->GetPassByIndex(0)->GetVertexShaderDesc(&vsPassDesc);
		vsPassDesc.pShaderVariable->GetShaderDesc(vsPassDesc.ShaderIndex, &vsDesc);

		HR(device->CreateInputLayout( positionLayout, ARRAYSIZE( positionLayout ), vsDesc.pBytecode, vsDesc.BytecodeLength, &VertexPosition::InputLayout ) );
	}

	{
		const D3D11_INPUT_ELEMENT_DESC positionTextureLayout[] =
		{
			{ "POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,    0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		HLSLEffect::createFromFile("LayoutPositionTexture.fx")->getEffect()->GetTechniqueByIndex(0)->GetPassByIndex(0)->GetVertexShaderDesc(&vsPassDesc);
		vsPassDesc.pShaderVariable->GetShaderDesc(vsPassDesc.ShaderIndex, &vsDesc);

		HR(device->CreateInputLayout( positionTextureLayout, ARRAYSIZE( positionTextureLayout ), vsDesc.pBytecode, vsDesc.BytecodeLength, &VertexPositionTexture::InputLayout ) );
	}
}

void InputLayouts::freeAll()
{
	ReleaseCOM(VertexPosition::InputLayout);
	ReleaseCOM(VertexPositionTexture::InputLayout);
}